<% if false # the license inside this if block pertains to this file -%>
  # Copyright 2017 Google Inc.
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
<% end -%>
<% if !property.to_request.nil? -%>
func expand<%= prefix -%><%= titlelize(property.name) -%>(d interface{}, arrayIndex map[string]int) (interface{}, error) {
    v, err := navigateValue(d, []string{<%= index2navigate(property.field, true) %>}, arrayIndex)
    if err != nil {
        return nil, err
    }
    <%= lines(property.to_request) -%>
}
<% elsif property.is_a?(Api::Type::Array) && property.item_type.is_a?(Api::Type::NestedObject) -%>
func expand<%= prefix -%><%= titlelize(property.name) -%>(d interface{}, arrayIndex map[string]int) (interface{}, error) {
    newArrayIndex := make(map[string]int)
    if arrayIndex != nil {
        for k, v := range arrayIndex {
            newArrayIndex[k] = v
        }
    }
<%   if property.array_num.nil? -%>
<%     rp = find_property(resource, property.field.split(".")) -%>
<%     unless rp.is_a?(Api::Type::Array) -%>
<%       raise "parameter(#{property.name}) is an array, but the matched property is not"-%>
<%     end -%>

    val, err := navigateValue(d, []string{<%= index2navigate(property.field, true) %>}, newArrayIndex)
    if err != nil {
        return nil, err
    }
    n := 0
    if val1, ok := val.([]interface{}); ok && len(val1) > 0 {
        n = len(val1)
    } else {
        return nil, nil
    }
<%   else -%>

    n := <%= property.array_num %>
<%   end -%>
<%   nested_properties = nested_properties(property) -%>
    req := make([]interface{}, 0, n)
    for i := 0; i < n; i++ {
<%   unless property.field.nil? -%>
        newArrayIndex["<%= to_schema_index(property.field) %>"] = i
<%   end -%>
        transformed := make(map[string]interface{})

        <%= lines(build_expand_properties(nested_properties, "d, newArrayIndex", prefix + titlelize(property.name), "transformed", true), 1) -%>
        if len(transformed) > 0 {
            req = append(req, transformed)
        }
    }

    return req, nil
}
<%   nested_properties.each do |prop| -%>

<%= lines(build_expand_method(resource, prefix + titlelize(property.name), prop), 1) -%>
<%   end -%>
<% elsif property.is_a?(Api::Type::NestedObject) -%>
func expand<%= prefix -%><%= titlelize(property.name) -%>(d interface{}, arrayIndex map[string]int) (interface{}, error) {
    req := make(map[string]interface{})

<%   nested_properties = nested_properties(property) -%>
    <%= lines(build_expand_properties(nested_properties, "d, arrayIndex", prefix + titlelize(property.name), "req", true), 1) -%>
    return req, nil
}
<%   nested_properties.each do |prop| -%>

<%= lines(build_expand_method(resource, prefix + titlelize(property.name), prop), 1) -%>
<%   end -%>
<% end # if property.is_a?(Api::Type::Array) -%>
